﻿package com.sgcc.nfc.lock.data.remote.dto.room

import com.google.gson.annotations.SerializedName
import com.sgcc.nfc.lock.data.remote.dto.lock.LockDto
import com.sgcc.nfc.lock.domain.model.Room
import com.sgcc.nfc.lock.domain.model.RoomStatus

data class RoomDto(
    @SerializedName("roomId") val roomId: Long?,
    @SerializedName("roomName") val roomName: String?,
    @SerializedName("regionId") val regionId: Long?,
    @SerializedName("regionName") val regionName: String?,
    @SerializedName("address") val address: String?,
    @SerializedName("longitude") val longitude: Double?,
    @SerializedName("latitude") val latitude: Double?,
    @SerializedName("status") val status: Int?,
    @SerializedName("lockCount") val lockCount: Int?,
    @SerializedName("openLockCount") val openLockCount: Int?,
    @SerializedName("malfunctionLockCount") val malfunctionLockCount: Int?,
    @SerializedName("locks") val locks: List<LockDto>?
) {
    fun toDomain(): Room = Room(
        roomId = roomId ?: 0L,
        roomName = roomName.orEmpty(),
        regionId = regionId,
        regionName = regionName,
        address = address,
        longitude = longitude,
        latitude = latitude,
        status = when (status) {
            0 -> RoomStatus.NORMAL
            1 -> RoomStatus.IN_USE
            2 -> RoomStatus.ALERT
            else -> RoomStatus.UNKNOWN
        },
        lockCount = lockCount ?: locks?.size ?: 0,
        openLockCount = openLockCount ?: 0,
        malfunctionLockCount = malfunctionLockCount ?: 0,
        locks = locks?.map(LockDto::toDomain).orEmpty()
    )
}
